package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.FlowNodeClassDao;
import net.mysoft.budgetsoft.system.domain.TFlowNodeClass;
import net.mysoft.budgetsoft.system.pagemodel.FlowNodeClass;
import net.mysoft.budgetsoft.system.service.FlowNodeClassService;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2013-07-08 17:26:13 
 */
@Service("flowNodeClassService")
public class FlowNodeClassServiceImpl implements FlowNodeClassService{
	@Resource
	private FlowNodeClassDao flowNodeClassDao;
	
	public FlowNodeClassDao getFlowNodeClassDao(){
		return this.flowNodeClassDao;
	}
	
	public void setFlowNodeClassDao(FlowNodeClassDao flowNodeClassDao){
		this.flowNodeClassDao = flowNodeClassDao;
	}
	
	@Override
	public void add(FlowNodeClass flowNodeClass){
		TFlowNodeClass t = new TFlowNodeClass();
		BeanUtils.copyProperties(flowNodeClass, t);
		t.setFnc_ID(UUID.randomUUID().toString());
		flowNodeClassDao.save(t);
	}
	
	@Override
	public void edit(FlowNodeClass flowNodeClass){
		TFlowNodeClass t = new TFlowNodeClass();
		BeanUtils.copyProperties(flowNodeClass, t);
		flowNodeClassDao.update(t);
	}
	
	@Override
	public void delete(FlowNodeClass flowNodeClass) {
		for (String id : flowNodeClass.getIds().split(",")) {
			Object t =  flowNodeClassDao.get(TFlowNodeClass.class, id);
			if (t != null) {
				 flowNodeClassDao.delete(t);
			}
		}
	}
	
	@Override
	public DataGrid datagrid(FlowNodeClass flowNodeClass){
		DataGrid dg = new DataGrid();
		String hql = "from TFlowNodeClass t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(flowNodeClass, hql, params);
		hql = addOrder(flowNodeClass, hql);
		String totalHql = "select count(*) " + hql;
		List<TFlowNodeClass> toList = flowNodeClassDao.find(hql, params, flowNodeClass.getPage(), flowNodeClass.getRows());
		List<FlowNodeClass> oList = new ArrayList<FlowNodeClass>();
		oList = changeModel(toList);
		dg.setTotal(flowNodeClassDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}
	
	private String addWhere(FlowNodeClass flowNodeClass, String hql, List<Object> params) {
		return hql;
	}
	
	private String addOrder(FlowNodeClass flowNodeClass, String hql) {
		if (flowNodeClass.getSort() != null) {
			hql += " order by " + flowNodeClass.getSort() + " " + flowNodeClass.getOrder();
		}
		return hql;
	}
	
	public List<FlowNodeClass> changeModel(List<TFlowNodeClass> tFlowNodeClass ){
		List<FlowNodeClass> list = new ArrayList<FlowNodeClass>();
		for(TFlowNodeClass to:tFlowNodeClass){
			FlowNodeClass o = new FlowNodeClass();
			BeanUtils.copyProperties(to, o);
			list.add(o);
		}
		return list;
	}
}
